package com.task.control.service;

import com.task.control.task.TaskDispatch;
import com.task.controller.annotation.AsyncTask;
import com.task.controller.annotation.CurrentTask;
import com.task.controller.annotation.Parser;
import com.task.controller.annotation.UITask;
import com.task.controller.param.impl.EmptyParamParser;
import com.task.controller.trusteeship.TaskRegister;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class ServiceAbility extends Ability implements TaskRegister {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
        super.onStart(intent);
        registerTask(this);
        TaskDispatch taskDispatch = TaskDispatch.getInstance();
        taskDispatch.init(this);
        taskDispatch.execute("notify/task");
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        unregister(this);
        super.onStop();
        HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }

    @AsyncTask(value = "async/param", isAsync = true)
    @Parser(cls = EmptyParamParser.class)
    private void asyncParam(Context context, String param) {
        getUITaskDispatcher().asyncDispatch(() -> {
            new ToastDialog(context).setText("异步任务测试：".concat(param)).show();
        });
        HiLog.info(LABEL_LOG, "异步任务调用测试");
    }

    @CurrentTask(value = "current/param")
    @Parser(cls = EmptyParamParser.class)
    private void currentParam(Context context, int param) {
        getUITaskDispatcher().asyncDispatch(() -> {
            new ToastDialog(context).setText("当前任务测试：".concat(String.valueOf(param))).show();
        });
        HiLog.info(LABEL_LOG, "当前任务调用测试");
    }

    @UITask(value = "ui/param", isAsync = true)
    @Parser(cls = EmptyParamParser.class)
    private void uiParam(Context context, double param) {
        String name = Thread.currentThread().getName();
        new ToastDialog(context).setText("UI任务测试，执行线程为：".concat(name).concat(" 的到的参数为：".concat(String.valueOf(param)))).show();
        HiLog.info(LABEL_LOG, "UI任务调用测试，当前执行线程名称：".concat(name));
    }

    @AsyncTask(value = "async/test", isAsync = true)
    @Parser(cls = EmptyParamParser.class)
    private void asyncTest() {
        getUITaskDispatcher().asyncDispatch(() -> {
            new ToastDialog(this).setText("异步任务测试").show();
        });
        HiLog.info(LABEL_LOG, "异步任务调用测试");
    }

    @CurrentTask(value = "current/test")
    @Parser(cls = EmptyParamParser.class)
    private void currentTest() {
        getUITaskDispatcher().asyncDispatch(() -> {
            new ToastDialog(this).setText("当前任务测试").show();
        });
        HiLog.info(LABEL_LOG, "当前任务调用测试");
    }

    @UITask(value = "ui/test", isAsync = true)
    @Parser(cls = EmptyParamParser.class)
    private void uiTest() {
        String name = Thread.currentThread().getName();
        new ToastDialog(this).setText("UI任务测试，执行线程为").show();
        HiLog.info(LABEL_LOG, "UI任务调用测试，当前执行线程名称：".concat(name));
    }
}